package ru.evga.inse.core.service.dict.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;

import ru.evga.inse.core.dao.DaoException;
import ru.evga.inse.core.dao.IDao;
import ru.evga.inse.core.dao.IResultContainer;
import ru.evga.inse.core.dao.dict.ICityDao;
import ru.evga.inse.core.domain.dict.City;
import ru.evga.inse.core.filter.dict.CityFilter;
import ru.evga.inse.core.service.dict.ICityService;
import ru.evga.inse.core.service.impl.GenericEntityService;

@Service
@Configurable
public class CityService 
	extends GenericEntityService<City>
	implements ICityService
    
{
    @Autowired
    private ICityDao m_cityDao;
    
    @Override
    protected IDao<Long, City> getDao()
    {
        return m_cityDao;
    }
    
    public City save(Long id, String name) throws DaoException
    {
        if(id != null)
        {
            City existCity = m_cityDao.find(id);
            existCity.setName(name);
            return m_cityDao.merge(existCity);
        }
        
        City city = new City();
        city.setName(name); 
        return m_cityDao.persist(city);
    }
    
    public IResultContainer<City> findRange(Integer firstResult, Integer maxResults,
        String orderBy, Boolean isAsc, CityFilter filter)
        throws DaoException
    {
        return m_cityDao.findRange(firstResult, maxResults, orderBy, isAsc, filter);
    }

}
